const sequelize = require('./config.js')
const userledger = require('./model/user.js')
const schoolInfo = require('./model/schoolInfo.js')
const userInfo = require('./model/userInfo.js')
const {student,studentManifestation,studentScores} = require('./model/tudent_sys.js')
const specialty = require('./model/specialty.js')
const {  grade, gradeClass } = require('./model/gradeAndClass.js')
const { rank_sys, rank} = require('./model/rank_table.js')
const roleSys = require('./model/role_sys.js')
const audit = require('./model/student_audit.js')


userInfo.belongsTo(userledger,{foreignKey: 'user_id'}); // userInfo 属于 userledger
userInfo.belongsTo(schoolInfo, {foreignKey: 'belong_to_school'});
specialty.belongsTo(schoolInfo, {foreignKey: 'specialty_school_id'});

// 显式声明双向关联
grade.hasMany(gradeClass, { foreignKey: 'grade_id' });
gradeClass.belongsTo(grade, { foreignKey: 'grade_id' });
grade.belongsTo(schoolInfo, {foreignKey: 'grade_school_id'});

rank.belongsTo(schoolInfo, {foreignKey: 'rank_school_id'});
rank.hasMany(rank_sys, { foreignKey: 'rank_id' });
rank_sys.belongsTo(rank, {foreignKey: 'belong_to_rank_id'});
rank_sys.belongsTo(roleSys, { foreignKey: 'role_sys_id' });
roleSys.hasMany(rank_sys, { foreignKey: 'role_sys_id' });
roleSys.belongsTo(schoolInfo, {foreignKey: 'role_school_id'});
roleSys.belongsTo(userInfo, {foreignKey: 'role_id'});
userInfo.hasMany(roleSys, { foreignKey: 'role_id', as: 'roleSys' });
student.belongsTo(specialty,{foreignKey:'specialty_id'})
audit.belongsTo(student,{foreignKey:'student_id'})
audit.belongsTo(userInfo,{foreignKey:'proposer_id'})
audit.belongsTo(userInfo,{foreignKey:'audit_person_id'})
audit.belongsTo(schoolInfo,{foreignKey:'school_id'})
studentManifestation.belongsTo(student,{foreignKey:'student_id'})
studentScores.belongsTo(student,{foreignKey:'student_id'})
// merchantInfo.belongsTo(userledger, {
// 	foreignKey: 'user_id',
// 	as: 'userInfo'  // 商家对应的用户信息
//   });
//   userledger.hasOne(userledger, {  // A 有一个 B
// 	foreignKey: 'Id',
// 	sourceKey: 'creator_id',
// 	as: 'creatorInfo'  // 创建者信息
//   });
//   productService.belongsTo(salesmanInfo,{foreignKey:'merchant_id'})
//   memberInfo.belongsTo(userledger,{foreignKey: 'user_id'});
//   productService.hasMany(appoint, {
//     foreignKey: 'service_id',  // 关联到 appoint 表的 service_id
//     sourceKey: 'Id'            // 关联到 productService 表的 Id
// });
// // reservation.hasMany(UserInfo)

// appoint.belongsTo(productService, {
//     foreignKey: 'service_id', // appoint 中的 service_id 字段
//     targetKey: 'Id', // productService 中的 Id 字段
//     as: 'productService' // 关联的别名
// });
// appoint.belongsTo(memberInfo, {
//     foreignKey: 'user_id', // appoint 中的 service_id 字段
//     targetKey: 'user_id', // productService 中的 Id 字段
// });
// appoint.hasMany(memberInfo)
// appoint.hasMany(merchantInfo)

// 加载更新表的时候跳过检验机制
 sequelize.sync({ alter: true, validate: false})

    .then(() => console.log('Database synced successfully'))

    .catch(err => console.error('Error syncing database:', err));

module.exports = {
	sequelize,
	userledger,
	schoolInfo,
	userInfo,
	specialty,
	grade,
	gradeClass,
	rank,
	rank_sys,
	roleSys,
	student,
	audit,
	studentManifestation,
	studentScores
	// merchantInfo,
	// // reservation,
	// productService,
	// memberInfo,
}